package com.cyl.manager.pms.service;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;

import io.swagger.annotations.ApiModelProperty;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.cyl.manager.pms.mapper.BrandMapper;
import com.cyl.manager.pms.mapper.ProductMapper;
import com.cyl.manager.pms.domain.entity.Brand;
import com.cyl.manager.pms.domain.entity.Product;
import com.cyl.manager.pms.domain.query.BrandQuery;

/**
 * 品牌管理Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class BrandService {
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private ProductMapper productMapper;
    
    @Autowired
    private RedisCache redisCache;
    
    /**
     * 查询品牌管理
     *
     * @param id 品牌管理主键
     * @return 品牌管理
     */
    public Brand selectById(Long id) {
        return brandMapper.selectById(id);
    }
    
	public Brand getCacheBrand(Long brandId) {
    	String key = "SYS_BRAND_ID_"+brandId;
    	Brand brand = redisCache.getCacheObject(key);
		if (brand == null) {
			brand = selectById(brandId);
			if (null != brand) {
	    	    redisCache.setCacheObject(key, brand, 10, TimeUnit.MINUTES);
			}
		}
		return brand;
	}
    

    /**
     * 查询品牌管理列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 品牌管理
     */
    public List<Brand> selectList(BrandQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<Brand> qw = new QueryWrapper<>();
        String nameLike = query.getNameLike();
        if (!StringUtils.isEmpty(nameLike)) {
            qw.like("name", nameLike);
        }
        
       
        String bz = query.getBz();
        if (!StringUtils.isEmpty(bz)) {
        	qw.like("bz", bz);
        }
        String lxr = query.getLxr();
        if (!StringUtils.isEmpty(lxr)) {
            qw.like("lxr", lxr);
        }
        String lxdh = query.getLxdh();
        if (!StringUtils.isEmpty(lxdh)) {
            qw.like("lxdh", lxdh);
        }
        String gsmc = query.getGsmc();
        if (!StringUtils.isEmpty(gsmc)) {
            qw.like("gsmc", gsmc);
        }
        String gsdz = query.getGsdz();
        if (!StringUtils.isEmpty(gsdz)) {
            qw.like("gsdz", gsdz);
        }
        String qtxx = query.getQtxx();
        if (!StringUtils.isEmpty(qtxx)) {
            qw.like("qtxx", qtxx);
        }
        
       
        
        Long qrDeptZbjId = query.getDeptZbjId();
        if(qrDeptZbjId!=null) {
            qw.eq("dept_zbj_id", qrDeptZbjId);
        }
        Integer sort = query.getSort();
        if (sort != null) {
            qw.eq("sort", sort);
        }
        Integer showStatus = query.getShowStatus();
        if (showStatus != null) {
            qw.eq("show_status", showStatus);
        }
        String logo = query.getLogo();
        if (!StringUtils.isEmpty(logo)) {
            qw.eq("logo", logo);
        }
        qw.orderByAsc("sort");
        return brandMapper.selectList(qw);
    }

    /**
     * 新增品牌管理
     *
     * @param brand 品牌管理
     * @return 结果
     */
    public int insert(Brand brand) {
    	redisCache.deleteObject("SYS_BRAND_ID_"+brand.getId());
    	brand.setDeptZbjId(SecurityUtils.getDeptZbjId());
        brand.setCreateTime(LocalDateTime.now());
        return brandMapper.insert(brand);
    }

    /**
     * 修改品牌管理
     *
     * @param brand 品牌管理
     * @return 结果
     */
    public int update(Brand brand) {
    	redisCache.deleteObject("SYS_BRAND_ID_"+brand.getId());
        return brandMapper.updateById(brand);
    }

    /**
     * 删除品牌管理信息
     *
     * @param id 品牌管理主键
     * @return 结果
     */
    public int deleteById(Long id) {
    	QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
    	queryWrapper.eq("brand_id", id);
    	if(productMapper.selectCount(queryWrapper)>0) {
            throw new RuntimeException("有商品关联不能删除");
    	}
    	redisCache.deleteObject("SYS_BRAND_ID_"+id);
        return brandMapper.deleteById(id);
    }
}
